/**
 * 权限管理
 */

/**
 * 添加/编辑视图模型
 */
interface IvisualAuthorizationDetailViewModel {
    /**编辑id */
    id?: string;
    /**一级权限分类枚举值 */
    authpStr?: string;
    /**
   * 可视化权限名称
   */
    name: string;
    /**
     * 一级权限分类
     */
    authorizationType: string;
    /**
     * 二级权限分类名称
     */
    secAuthorizationType: string;
    /**
     * 二级权限分类标签
     */
    secAuthorizationTypeTag: string;
    /**
     * 权限等级
     */
    level: string;
    /**
     * 标签
     */
    tag: Array<string>;

    /**
   * 保存事件
   */
    click: (authorizationType: string, secAuthorizationType: string, secAuthorizationTypeTag: string) => void;

}

/**
 * 视图模型
 */
interface IvisualAuthorizationManagentViewModel extends IBaseViewModel<IvisualAuthorizationManagentQueryTerms> {
    /**编辑id */
    id: string;
    /**
    * 添加/编辑视图
    */
    detail: IvisualAuthorizationDetailViewModel;
    /**
     * 一级权限分类
     */
    authorizationType: string;
    /**
     * 权限树
     */
    TreeData: any;
    /**
     * 添加权限
     */
    addAuthorization: (authorizationType: string, authorizationTypeStr: string, secAuthorizationType: string, secAuthorizationTypeTag: string) => void;
    /**
     * 编辑权限
     */
    editAuthorization: (id: string, name: string, authorizationType: string, secAuthorizationType: string, secAuthorizationTypeTag: string, level: string, tag: Array<string>) => void;
    /**
     * 删除权限
     */
    delAuthorization: (id: string) => void;
    /**一级权限名称枚举 */
    authorizationTypeArr: ValueListItemResponse[];
    /**
     * 权限等级枚举
     */
    authorizationLevel: ValueListItemResponse[];
    /**
     * tag
     */
    tagListData: { tags: string, index?: number }[];
    /**
     * 添加tag
     */
    ifArrVal: (arr, obj) => void;
    addTag: () => void;
    /** 删除Tag*/
    deleteTag: (index: number) => void;
    /**
     * 标签
     */
    tag: Array<string>;
    /**二级权限分类名称输入提示 */
    SecAuthorizationTypeSelect: (selected: angucomplete<getSecAuthTypeListItemReponse>) => void;
    /**二级权限分类名称提示 */
    searchSecAuthorizationType: (value: string) => void;
    /**是否编辑 */
    editB: boolean;

}
/**
 * 检索
 */
interface IvisualAuthorizationManagentQueryTerms {

}

class VisualauthorizationManagentController extends ListController<IvisualAuthorizationManagentViewModel, IvisualAuthorizationManagentQueryTerms> {

    constructor($scope: IvisualAuthorizationManagentViewModel, private visualAuthorizationService: IvisualAuthorizationService, private valueService: valueService, private $ngBootbox: BootboxService, private $timeout: ng.ITimeoutService) {
        super($scope);
        this.init();

    }

    init(): void {

        super.init();
        this.$scope.addAuthorization = this.showAddAuthorizationDialog;

        this.loadData();
        /**一级权限名称枚举 */
        this.$scope.authorizationTypeArr = [];
        /**
         * 权限等级枚举
         */
        this.$scope.authorizationLevel = [];

        this.$scope.delAuthorization = this.delAuthorization;

        this.$scope.editAuthorization = this.showEditAuthorizationDialog;

        /**
         * 添加Tag
         */
        this.$scope.ifArrVal = this.ifArrVal;
        this.$scope.addTag = this.addTag;
        /**删除Tag */
        this.$scope.deleteTag = this.deleteTag;
        /**
         * 标签
         */
        this.$scope.tag = [];
        this.$scope.tagListData = [];
        this.loadDropDown();//加载枚举
        /**二级权限分类名称输入提示 */
        this.$scope.SecAuthorizationTypeSelect = this.SecAuthorizationTypeSelect;
        /**二级权限分类名称提示 */
        this.$scope.searchSecAuthorizationType = this.searchSecAuthorizationType;
       
            var Zswiper = new Swiper('.swiper-container', {
                pagination: '.swiper-pagination',
                nextButton: '.swiper-button-next',
                prevButton: '.swiper-button-prev',
                observer: true,//修改swiper自己或子元素时，自动初始化swiper
                //observeParents: true,//修改swiper的父元素时，自动初始化swiper
                slidesPerView: 'auto',
                centeredSlides: false,
                paginationClickable: false,
                spaceBetween: 10,
               
            })
            this.$timeout(() => {
                console.log(Zswiper.prevButton);
                /**增加class */
                Zswiper.prevButton.addClass("swiper-button-disabled");
                /**移除class */
                Zswiper.nextButton.removeClass("swiper-button-disabled");
                /**设置过渡效果 */
                 Zswiper.setWrapperTransition(0);
                 /**设置位移位0 显示第一页 */
                 Zswiper.setWrapperTranslate(0);
                 /**使用jquery方法设置 */
        //    $(".swiper-wrapper").css('transform','translate3d(0px, 0px, 0px)');
        },1000)
        
    }

    /**
     * 加载权限树
     */
    loadData(): void {
        this.visualAuthorizationService.getList(null,null).then(result=>{
            this.$scope.TreeData=result.data.data;
            // this.$scope.clientTree=result.data.data[0];
            // this.$scope.inquiryTree=result.data.data[1];
        })
    }
    /**删除树节点 */
    delAuthorization = (id: string) => {
        this.$ngBootbox.confirm("是否删除该权限？").then(e => {
            this.visualAuthorizationService.delete(id).then(result => {
                this.$ngBootbox.alert("删除成功！");
                this.reload();
            })
        });
    };

    /**
     * 二级权限分类名称输入提示
     */
    SecAuthorizationTypeSelect = (selected: angucomplete<getSecAuthTypeListItemReponse>) => {
        if (selected) {
            this.$scope.detail.secAuthorizationType = selected.description.secAuthorizationType;
            this.$scope.detail.secAuthorizationTypeTag = selected.description.secAuthorizationTypeTag;
            this.searchSecAuthorizationType;
        }
    }

    /**
     *  二级权限分类名称提示
     */
    searchSecAuthorizationType = (value: string) => {
        return this.visualAuthorizationService.getSecAuthTypeList(this.$scope.detail.authpStr, value, 0, -1).then((res) => {
            return res.data.data;
        });
    }

    /**
     * 下拉加载
     */
    loadDropDown(): void {
        this.$scope.authorizationTypeArr = this.valueService.getVisualAuthorzition().data;
        this.$scope.authorizationLevel = this.valueService.getVisualAuthorizationLevel().data;
        //console.log(this.$scope.authorizationLevel)
    }
    /**判断tag是否重复 */
    ifArrVal = (arr, obj) => {//多维数组判断是否存在某值
        var i = arr.length;
        while (i--) {
            if (arr[i].tags === obj) {
                this.$ngBootbox.alert("tag已存在！");
                return;
            }
        }
        let index = this.$scope.tagListData.length + 1;
        this.$scope.tagListData.push({
            tags: obj,
            index: index++
        });
        return false;
    }
    /**
     * 添加Tag
     */
    addTag = () => {
        if ($("#AuthorizationTag").val() == "") {
            this.$ngBootbox.alert("请添加tag！");
            return;
        }
        /**去除空格 */
        let tags = $("#AuthorizationTag").val().replace(/\s/g, "");
        /**判断tag是否重复 */
        this.$scope.ifArrVal(this.$scope.tagListData, tags);
    }


    /**
    * 删除tag
    */
    deleteTag = (index: number) => {
        this.$scope.tagListData.splice(index - 1, 1);
        this.$scope.tagListData.forEach((item, index) => {
            this.$scope.tagListData[index].index = index + 1;
        });
    }

    /**
     * 显示添加权限窗口
     */
    showAddAuthorizationDialog = (authorizationType: string, authorizationTypeStr: string, secAuthorizationType: string, secAuthorizationTypeTag: string) => {
        this.$scope.title = "添加权限";
        this.$scope.editB = false;
        if (secAuthorizationType != "") {
            $("#secAuthorizationType_value").val(secAuthorizationType);
        }
        /**清空 */
        this.$scope.tagListData = [];
        this.$scope.detail = {
            authpStr: authorizationType,
            name: "",
            authorizationType: authorizationTypeStr,
            secAuthorizationType: $("#secAuthorizationType_value").val(),
            secAuthorizationTypeTag: secAuthorizationTypeTag,
            level: "",
            tag: [],
            click: this.addAuthorization
        };
        //console.log(this.$scope.detail);
    }
    /**
     * 添加权限
     */
    addAuthorization = () => {
        //自定义二级分类名称验证
        //console.log(this.$scope.tagListData)
        if (this.$scope.tagListData.length == 0) {

            // $("#AuthorizationTag").addClass("error");
            $("#AuthForm").validate({
                rules: {
                    secAuthorizationType: {
                        required: true
                    }
                }
            });
        }

        /**不为空去除验证 */
        if ($("#secAuthorizationType_value").val() == "") {
            $("#secAuthorizationType_value").removeClass("error");
            $("secAuthorizationType_value-error").remove();
        }
        if (!$("#AuthForm").valid()) { return };
        //console.log(this.$scope.detail);

        this.$ngBootbox.confirm("确定保存该数据吗？").then(e => {
            this.$scope.tag = [];
            this.$scope.tagListData.forEach((item, index) => {
                this.$scope.tag.push(item.tags);
            })
            console.log(this.$scope.tag);
            if (this.$scope.tag.length == 0) {
                $("#AuthorizationTag").addClass("error");
                this.$ngBootbox.alert("请添加tag！");
                return;
            } else {
                this.visualAuthorizationService.add(
                    this.$scope.detail.name,
                    this.$scope.detail.authpStr,
                    $("#secAuthorizationType_value").val(),
                    this.$scope.detail.secAuthorizationTypeTag,
                    this.$scope.detail.level,
                    this.$scope.tag
                ).then(result => {
                    this.reload();
                    $('#myModal').modal('hide');
                })
            }
        });
    }


    /**
     * 显示编辑权限窗口
     */
    showEditAuthorizationDialog = (id: string, name: string, authorizationType: string, secAuthorizationType: string, secAuthorizationTypeTag: string, level: string, tag: Array<string>) => {
        this.$scope.title = "编辑权限";
        this.$scope.editB = true;
        var validator = $('#AuthForm').validate();
        validator.form();
        validator.hideErrors();
        $(".error").removeClass("error");

        if (secAuthorizationType != "") {
            $("#secAuthorizationType_value").val(secAuthorizationType);
        }

        this.$scope.detail = {
            id: id,
            name: name,
            authorizationType: authorizationType,
            secAuthorizationType: secAuthorizationType,
            secAuthorizationTypeTag: secAuthorizationTypeTag,
            level: level,
            tag: [],
            click: this.editAuthorization
        };
        /**tag赋值 */
        this.$scope.tagListData = [];
        let indexs = this.$scope.tagListData.length + 1;
        tag.forEach((item, index) => {
            this.$scope.tagListData.push({
                tags: item,
                index: indexs++
            })
        });

    }
    /**编辑树节点 */
    editAuthorization = () => {

        // if (this.$scope.tagListData.length != 0) {

        //     $("#AuthForm").validate({
        //         rules: {

        //             AuthorizationTag: {
        //                 required: false
        //             }
        //         }
        //     });
        // }
        // else{
        //     var validator = $('#AuthForm').validate();
        //     validator.form();
        //     // $('#AuthorizationTag').validate().hideErrors();
        //     $("#AuthorizationTag").removeClass("error");
        // }

        if (!$("#AuthForm").valid()) { return };

        this.$ngBootbox.confirm("确定保存该数据吗？").then(e => {
            this.$scope.tag = [];
            this.$scope.tagListData.forEach((item, index) => {
                this.$scope.tag.push(item.tags);
            })
            console.log(this.$scope.tag.length)
            if (this.$scope.tag.length == 0) {
                $("#AuthorizationTag").addClass("error");
                this.$ngBootbox.alert("请添加tag！");
                return;
            } else {
                this.visualAuthorizationService.edit(
                    this.$scope.detail.id,
                    this.$scope.detail.name,
                    this.$scope.tag
                ).then(result => {
                    this.reload();
                    $('#myModal').modal('hide');
                });
            }

        });
    }


}
angular.module("tms").controller('visualauthorizationManagentController', ['$scope', 'visualAuthorizationService', 'valueService', '$ngBootbox', '$timeout', VisualauthorizationManagentController]);